package com.zhiyou100.controller;

import com.zhiyou100.service.LoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    LoginService LoginService;

    @RequestMapping("/behind")
    public String behindLogin(String tel,String password,HttpServletRequest request){
        //使用Shiro登陆进行认证授权
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new UsernamePasswordToken(tel,password));
            System.out.println("登陆成功!");
            HttpSession session = request.getSession();
            session.setAttribute("username",tel);
            return "redirect:/order/list.do";
        }catch (AuthenticationException e){
            System.out.println("用户名或密码错误");
        }
        return "redirect:/behind/behindIndex.jsp";

    }
}
